//
//  OrderDetailAAViewCell.m
//  DMGME
//
//  Created by MC on 15/9/26.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "OrderDetailAAViewCell.h"

#import "DMOrderDetailModel.h"

#import "UIImageView+WebCache.h"

#import "OrderDetaiABlViewCell.h"

#import "SubOrederdetailMode.h"

@implementation OrderDetailAAViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UILabel *willPickup = [[UILabel alloc] initWithFrame:CGRectMake(2, 1, 109, 21)];
        _willPickup = willPickup;
        willPickup.font = [UIFont systemFontOfSize:12];
        willPickup.textColor = [UIColor redColor];
        [self.contentView addSubview:willPickup];
        
        UIImageView *linetopss = [[UIImageView alloc] initWithFrame:CGRectMake(0, willPickup.frame.size.height+1, Width, 0.5)];
        
        linetopss.alpha = 0.4;
        
        linetopss.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:linetopss];

        
        UITableView *centerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 21, Width, 85) style:UITableViewStylePlain];
        
        centerTableView.delegate = self;
        centerTableView.dataSource = self;
        _centerTableView = centerTableView;
        [_centerTableView registerClass:[OrderDetaiABlViewCell class] forCellReuseIdentifier:@"OrderDetaiABlViewCell"];
        _centerTableView.scrollEnabled = NO;
        [self.contentView addSubview:centerTableView];
        UILabel *totalMoney = [[UILabel alloc] initWithFrame:CGRectMake(rects.size.width-80, 110+centerTableView.frame.size.height, 80, 21)];
        _totalMoney = totalMoney;
        totalMoney.font = [UIFont systemFontOfSize:14];
        totalMoney.textColor = [UIColor grayColor];
        
        [self.contentView addSubview:totalMoney];
        UILabel *get_finishtime = [[UILabel alloc] init];
        _get_finishtime = get_finishtime;
        get_finishtime.font = [UIFont systemFontOfSize:14];
        get_finishtime.textColor = [UIColor grayColor];
        
        [self.contentView addSubview:get_finishtime];
        
        UILabel *order_time = [[UILabel alloc] init];
        
        _order_time = order_time;
        order_time.font = [UIFont systemFontOfSize:14];
        order_time.textColor = [UIColor grayColor];
        [self.contentView addSubview:order_time];
        
        UILabel *order_sn = [[UILabel alloc] init];
        _order_sn = order_sn;
        order_sn.font = [UIFont systemFontOfSize:14];
        order_sn.textColor = [UIColor grayColor];
        [self.contentView addSubview:order_sn];
        UIImageView *line = [[UIImageView alloc] init];
        line.tag = 12;
        line.backgroundColor = [UIColor lightGrayColor];
        line.alpha = 0.4;
        [self.contentView addSubview:line];
        
        UIImageView *linetop = [[UIImageView alloc] init];
        linetop.tag = 13;
        linetop.backgroundColor = [UIColor lightGrayColor];
        linetop.alpha = 0.4;
        [self.contentView addSubview:linetop];
        
        UIButton *centeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        
        
        [centeBtn setBackgroundImage:[UIImage imageNamed:@"confirmGoodsButton Copy"] forState:UIControlStateNormal];
        _centeBtn = centeBtn;
        [self.contentView addSubview:centeBtn];
        
        
        UIButton *gukeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [gukeBtn setBackgroundImage:[UIImage imageNamed:@"contactButton.png"] forState:UIControlStateNormal];
        _gukeBtn = gukeBtn;
        [self.contentView addSubview:gukeBtn];
        
        UIButton *yanchangBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        
        [yanchangBtn setBackgroundImage:[UIImage imageNamed:@"delayButton.png"] forState:UIControlStateNormal];
        _yanchangBtn = yanchangBtn;
        [self.contentView addSubview:yanchangBtn];
        

        [self.contentView addSubview:centeBtn];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
    //    _centerTableView.delegate = self;
    //    _centerTableView.dataSource = self;
    //
    //    [_centerTableView registerNib:[UINib nibWithNibName:@"OrderDetaiABlViewCell" bundle:nil] forCellReuseIdentifier:@"OrderDetaiABlViewCell"];
    
}

#pragma mark - tableViewMethod
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    CGRect rect = CGRectMake(0, 26, [[UIScreen mainScreen]bounds].size.width,85*self.dmModel.goods.count);

    _centerTableView.frame = rect;
    _centeBtn.frame = CGRectMake(Width-75, 105+CGRectGetMaxY(_centerTableView.frame), 70, 30);
    _totalMoney.frame = CGRectMake(rects.size.width-80, CGRectGetMaxY(_centerTableView.frame)+5, 80, 21);
    _get_finishtime.frame = CGRectMake(15, 40+CGRectGetMaxY(_centerTableView.frame), 230, 21);
    _order_time.frame = CGRectMake(15, 60+CGRectGetMaxY(_centerTableView.frame), 260, 21);
    _order_sn.frame = CGRectMake(15, 80+CGRectGetMaxY(_centerTableView.frame), 175, 21);
    _gukeBtn.frame = CGRectMake(_centeBtn.frame.origin.x-70, 105+CGRectGetMaxY(_centerTableView.frame), 60, 30);
    _yanchangBtn.frame = CGRectMake(_gukeBtn.frame.origin.x-85, 105+CGRectGetMaxY(_centerTableView.frame), 75, 30);

    UIImageView *linetop = (UIImageView *)[self.contentView viewWithTag:13];
    linetop.frame = CGRectMake(0, 10+CGRectGetMaxY(_centerTableView.frame)+20, Width, 0.5);
    UIImageView *lineend = (UIImageView *)[self.contentView viewWithTag:14];
    lineend.frame = CGRectMake(0, self.frame.size.height-15, Width, 0.5);
    _centerTableView.separatorStyle = UITableViewCellSelectionStyleNone;
//    NSLog(@"_centerTableView == %lf",_centerTableView.frame.size.height);
    
    return self.dmModel.goods.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"OrderDetaiABlViewCell";

    OrderDetaiABlViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
    
    cell.subModel = [SubOrederdetailMode modelWithDic:_dmModel.goods[indexPath.row]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    CGFloat height = CGRectGetMaxY(_order_sn.frame) ;

       return  85;
}
+(OrderDetailAAViewCell *)cellWithTableView:(UITableView *)tableView
{
    static  NSString*ident=@"cell";
    OrderDetailAAViewCell *cell=[tableView  dequeueReusableCellWithIdentifier:ident];
    
    if (!cell) {
        cell=[[OrderDetailAAViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ident];
        
    }
    cell.contentView.backgroundColor = [UIColor clearColor];
   
    return cell;
}


-(void)setDmModel:(DMOrderDetailModel *)dmModel
{
    _dmModel = dmModel;
    
    _willPickup.text = [NSString stringWithFormat:@"等待取货"];
    _get_finishtime.text = [NSString stringWithFormat:@"订单生成时间:%@",[self timeStamaps:dmModel.order_time]];
//    NSLog(@"%@~%@",[self timeStamaps:dmModel.get_starttime],[self timeStamaps:dmModel.get_finishtime]);
    NSString *string1 = [[self timeStamaps:dmModel.get_starttime] substringToIndex:16];
    NSString *string2 = [[self timeStamaps:dmModel.get_finishtime] substringWithRange:NSMakeRange(11, 5)];
//    NSLog(@"%@~%@",string1,string2);
    
    _order_time.text = [NSString stringWithFormat:@"取货时间:%@",[NSString stringWithFormat:@"%@~%@",string1,string2]];
    _order_sn.text = [NSString stringWithFormat:@"订单号:%@",dmModel.order_sn];
    float fl = dmModel.all_total.floatValue + dmModel.coupon.floatValue;
    _totalMoney.text = [NSString stringWithFormat:@"￥%0.2f",fl];
    
    [_centerTableView reloadData];
    
}

-(NSString *)timeStamaps:(NSString *)str
{
    //时间戳转化为时间字符串
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:[str longLongValue]];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:MM:ss"];
    NSString *string = [formatter stringFromDate:date];
    NSLog(@"date1:%@",str);
    return string;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
